const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    // slide
    value:0,
    maxvalue:'',




    page:1,
    limit:10,

    list:[],
    music:'',
    number:'-1',


    starttime:'00:00',
    endtime:'',
    // 搜索姓名
    name:'',

    // 限制多次点击
    goplay:true,

    bookbag:''
  },
// ----
  // 姓名搜索
  name(e){
    let that = this
    that.setData({
      name:e.detail.value
    })
    that.musiclist()
  },
  delete(){
    let that = this
    that.setData({
      name:''
    })
    that.musiclist()
  },
// ----
  goplay:function(e){
    let that = this
    if(that.backmusic){
      that.backmusic.pause()
    }
    
    setTimeout(()=>{
      that.setData({
        goplay:true
      })
    },2000)
    if(!that.data.goplay){
      return false;
    }
    that.setData({
      goplay:false
    })
    wx.showLoading({
      title: '跳转中',
    })

    that.setData({
      number:'-1',
    })
    console.log(e);
    let hbdetail = {
      image:e.currentTarget.dataset.image,
      music:e.currentTarget.dataset.music,
      name:e.currentTarget.dataset.name,
      goods_id:e.currentTarget.dataset.goods_id,
    }
    wx.setStorage({
      key: 'tmusic',
      data: hbdetail,
      success: function(res) {
        wx.hideLoading()
        wx.navigateTo({
          url: '../smplay/index'
        })
      },
    })
  },


  // 防止冒泡
  mp:function(){
    return false
  },
  // 点击进度条跳动
  sliderchange(event){
    console.log(event.detail)
    this.backmusic.seek(event.detail.value)
  },
  sliderchanging(event){
    console.log(event.detail)
    this.backmusic.seek(event.detail.value)
  },

  // 开始播放
  audioPlay(e){
    console.log(e)
    let that = this

    if(!e.currentTarget.dataset.music){
      wx.showToast({
        title:'音频地址不正确',
        icon:'none',
        duration:2000
      })
      return false
    }

    that.setData({
      music:e.currentTarget.dataset.music,
      number:e.currentTarget.dataset.index,
    })
    that.music()
    that.backmusic.onTimeUpdate(()=>{
      //console.log(that.backmusic.duration)  //总时长
      //console.log(that.backmusic.currentTime)//进行
        // 计算时长
        let sumtime = that.backmusic.duration;
        let newtime = that.backmusic.currentTime
        let width = newtime / sumtime * 100 + '%'
        // 总时间
        let sumtimem = parseInt(sumtime / 60);
        let sumtimes = parseInt(sumtime % 60);
        if (sumtimem < 10) {
          sumtimem = "0" + sumtimem
        }
        if (sumtimes < 10) {
          sumtimes = "0" + sumtimes
        }
        let newtimem = parseInt(newtime / 60);
        let newtimes = parseInt(newtime % 60);


        if (newtimem < 10) {
          newtimem = "0" + newtimem
        }
        if (newtimes < 10) {
          newtimes = "0" + newtimes
        }

        let end = sumtimem + ':' + sumtimes;
        let start = newtimem + ':' + newtimes
        console.log(end)
        console.log(start)

      that.setData({
        value:that.backmusic.currentTime,
        maxvalue:that.backmusic.duration,
        starttime:start,
        endtime:end
      })
    })

    // 循环播放
    that.backmusic.onEnded(function(){
      that.music()
    })

  },
  // 停止播放
  audioPause(){
    let that = this
    that.setData({
      number:'-1',
    })
    that.backmusic.pause()
  },



  // 创建背景音乐
  music(){
    let that = this
    that.backmusic = wx.getBackgroundAudioManager()
    that.backmusic.src =  that.data.music
    that.backmusic.title="测试"
  },


  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var scan_url = decodeURIComponent(options.q);
    var index = scan_url.lastIndexOf("\/");  
    var pid = scan_url.substring(index + 1, scan_url.length);
    console.log(pid)


    this.musiclist(pid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
 
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
 
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    let page = that.data.page
    page++
    that.setData({
      page:page
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },


 
    
 // 获取列表
 musiclist:function(q){
   let that = this
   wx.request({
     url: app.api + app.bookbag,
     data: {
       id:q
     },
     method: 'POST',
     success: function(res) {
       console.log(res)
       if(res.data.code == 0){
        that.setData({
          booxbag:res.data.data.bookbag,
          list:res.data.data.goods,
        })
       }else{
          wx.showToast({
            title: '书包查询失败',
            icon:"none",
            duration:2000
          })
       }
     },
     fail:function(){
      wx.showToast({
        title: '书包查询失败',
        icon:"none",
        duration:2000
      })
    }
   })
 }
})